<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Cookie;

class Release extends Controller
{
    public function publish(){
    	$lng = Cookie::get('lngX');
    	$lat = Cookie::get('latY');
    	Cookie::set('lng',$lng);
    	Cookie::set('lat',$lat);
    	$id = Session::get('id');
		$cate_id = $_GET['cate_id'];
		$go_time = $_GET['go_time'];
		$end_time = $_GET['end_time'];
		$lng = $_GET['lng'];
		$lat = $_GET['lat'];

    	if($id){
    		/*发布消息时检测用户时候完善个人信息*/
    		$info = Db::table("tp_user")->where(array("id"=>$id))->find();


			if($info['name'] != '' && $info['img'] != '' && $info['age'] != 0 && $info['tel'] != ''){
                //用户信息已完善
				$tag = Db::table("tp_tag")->select();
				$this->view->assign("lng",$lng);
				$this->view->assign("lat",$lat);
				$this->view->assign("info",$info);
				$this->view->assign("tag",$tag);
				$this->view->assign("cate_id",$cate_id);
				$this->view->assign("go_time",$go_time);
				$this->view->assign("end_time",$end_time);
				return $this->view->fetch();
			}else{
    			Session::set('url',$_SERVER['REQUEST_URI']);
				
				echo "<script>alert('请先完善个人信息再发布活动！（感叹号的为必填信息）');window.location.href='/index/personal/photo?id=$id'</script>";
			}
    	}else{
    		Session::set('url',$_SERVER['REQUEST_URI']);
    		echo "<script>alert('您还未登录，请先登录！');window.location.href='/index/login/login_deng'</script>";
    	}
    }

	public function card(){
		$id = Session::get('id');
		$user_info = Db::table("tp_user")->field("id,name,age,is_rz,img")->where(array("id"=>$id))->find();
		$user_id = $user_info['id'];

		$active_info = Db::table('tp_info')->where(array('user_id'=>$user_id))->select();
		$this->view->assign('user_info',$user_info);
		$this->view->assign('active_info',$active_info);

		return $this->view->fetch();
	}
    public function card_me(){
    	$id = $_GET['id'];
    	$info = Db::table('tp_info')->where(array('id'=>$id))->select();

    	$this->view->assign('info',$info);
    	return $this->view->fetch();
    }
	public function addthing(){
		$id = Session::get('id');
		$data['user_id'] = $_POST['id'];
		$data['cate_id'] = $_POST['cate_id'];
		$data['go_time'] = strtotime($_POST['go_time']);
		$data['end_time'] = strtotime($_POST['end_time']);
		$data['lng'] = $_POST['lng'];
		$data['lat'] = $_POST['lat'];
//		$data['address'] = $_POST['address'];

		$data['create_time'] = time();
		$data['content'] = $_POST['content'];
		$data['other_tag'] = $_POST['addtag'];
		$data['only_rz'] = $_POST['only_rz'];
		
		$data['max_people'] = $_POST['max_people']?$_POST['max_people']:0;
		$data['pay_type'] = $_POST['pay_type'];
		$data['deposit'] = $_POST['deposit'];
		$data['tag_id'] = $_POST['tag'];
		
		//AA制时活动发起人需要缴纳的押金
		$pay_money = $_POST['pay_money'];
		/*if($tag){
			$tag =implode(',',$_POST['tag']);
		}else{
			$tag = 0;
		}*/
		$data['status'] = 1;
		$data['photo'] = $_POST['photo'];

		//查询活动发起人的账户余额，判断是否小于押金
		$user_money = Db::table('tp_user')->field('money')->where('id',$id)->find();
		$money = $user_money['money'];
		
//		var_dump($money.",".$pay_money);exit;
		if($money >= $pay_money){
			$change_money = $money - $pay_money;

				if($_POST['pay_type'] == 0){
					if($_POST['content'] != '' && $_POST['photo'] != '' && $_POST['lng'] != '' && $_POST['lat'] != '' && $_POST['max_people'] > 0 && $_POST['deposit'] != '' && $_POST['go_time'] != '' && $_POST['end_time'] != ''){
						$add = Db::table("tp_info")->insert($data);
						if($change_money == $money){
							$res = 1;
						}else{
							$res = Db::table('tp_user')->where('id',$id)->setField('money',$change_money);
						}
						if($res){
							return 1;
						}else{
							return -1;
						}
					}else{
						//请确认活动信息是否完整(出行时间，结束时间，内容，照片，位置，金额必填)
//						return "内容：".$_POST['content'].';照片：'.$_POST['photo'].';经纬度：'.$_POST['lng'].','.$_POST['lat'].';最大人数：'.$_POST['max_people'].';押金：'.$_POST['deposit'].';出行时间：'.$_POST['go_time'].';结束时间'.$_POST['end_time'];
						return -2;
					}
				}else{
					if($_POST['content'] != '' && $_POST['photo'] != '' && $_POST['lng'] != '' && $_POST['lat'] != '' && $_POST['max_people'] > 0 && $_POST['go_time'] != '' && $_POST['end_time'] != ''){
						$add = Db::table("tp_info")->insert($data);
						if($add){
							return 1;
						}else{
							return -1;
						}
					}else{
						//请确认活动信息是否完整(出行时间，结束时间，内容，照片，位置，金额必填)
						return -2;
					}
				}

		}else{
			//您的账户余额不足以缴纳押金，请及时充值
			return -3;
		}
	}    
	
	
	public function mcupload(){
		if($_FILES['fb_img']['tmp_name']){
			$pic = $this ->request ->file('fb_img');
			$info = $pic -> move(ROOT_PATH . 'public/tmp/uploads/');//生成图片

			if($info){
				$pic = '/tmp/uploads/'.$info->getSaveName();
				/*$image = \think\Image::open(".".$pic);
				// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
				$image->thumb(230,185,\think\Image::THUMB_CENTER)->save(".".$pic);*/
				echo  $pic;exit;
			}else{
				echo "上传失败";exit;
			}

		}
	}
	public function ditu(){
		return $this->view->fetch("search_city");
	}
	public function dizi(){
		return $this->view->fetch("search_city_list");
	}
	/**
	 * 百度地图webApi(Geocoding)
     * 通过经纬度获取详细地址
	 * */
	function get_address(){
	    $get_lat = $_POST['get_lat'];
	    $get_lng = $_POST['get_lng'];
        $url = "http://api.map.baidu.com/geocoder/v2/?location=".$get_lat.",".$get_lng."&output=json&pois=1&ak=P761cP7GyvYG5bW334TR7aXIRePEpMX5";
        $data = file_get_contents($url);
        $info = json_decode($data,true);
        $address = $info['result']['pois']['0']['name'];
        return json(['code'=>200,'address'=>$address]);
	}
}
